<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="hAdd(2,'')">添加权限</el-button>
        </div>
        <el-table
          border
          :data="list"
          row-key="id"
          :default-expand-all="true"
        >
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template slot-scope="{ row }">
              <el-button v-if="row.type===1" type="text" @click="hAdd(2,row.id)">添加</el-button>
              <el-button type="text">编辑</el-button>
              <el-button type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 弹出层 -->
      <el-dialog
        title="提示"
        :visible.sync="showDialog"
        width="30%"
        @close="hdel"
      >
        <el-form ref="formData" :model="formData" :rules="rules" label-width="100px" class="demo-ruleForm">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-text="启用"
              active-value="1"
              inactive-text="不启用"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="hSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { tranListToTreeData } from '@/utils/index.js'
import { getPermissionList, addPermission } from '@/api/permissions'
export default {
  data() {
    return {
      list: [],
      showDialog: false, // 是否显示弹层
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = tranListToTreeData(res.data)
    }, // 获取权限的数据
    async loadPermission() {
      const res = await addPermission(this.formData)
      if (res.success) {
        this.$message.success(res.message)
        this.showDialog = false
      }
    }, // 新增权限
    hAdd(type, id) {
      this.formData.type = type
      this.formData.pid = id
      this.showDialog = true
      this.loadPermissionList()
    },
    hSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loadPermission()
        } else {
          return false
        }
      })
    },
    hdel() {
      this.$refs.formData.resetFields()
      this.formData = {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      }
    }
  }
}

</script>
